package cn.laixueit.controller;

import cn.laixueit.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

@Controller
@RequestMapping("/hello")
public class HelloController {

    HelloController(){
        init();
    }

    HashMap map =  new HashMap();

    public void init (){
        map.put("1",new User());
        map.put("2",new User());
        map.put("3",new User());
        map.put("4",new User());
        map.put("5",new User());
        map.put("6",new User());
    }


    @RequestMapping("/hi")
    public ModelAndView hi(ModelAndView mv){

        mv.setViewName("/index.jsp");
        mv.addObject("msg","hi hello");

        return mv;
    }


    @RequestMapping("/a")
    public String a(Model mv){

        mv.addAttribute("msg","张三");

        return "index";
//        /WEB-INF/pages/index.jsp
    }


    @RequestMapping("/getuser")
    public String getuser(Model model){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setAddress("武汉");
        model.addAttribute("user",user);
        return "index";
    }

    @RequestMapping("/getById")
    public String getById(Model model,Integer id){
        User u = (User) map.get(id);
        model.addAttribute("id",id);
        return "index";
    }

    @RequestMapping("/getname")
    public String getname(Model model,String name){
        model.addAttribute("name",name);
        return "index";
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String username,String pwd){

        if("admin".equals(username) && "123".equals(pwd)){
            return "index";
        }
        return "err";
    }
}
